﻿<?php

/**
 * User: yangcheng
 */
header ( "Content-type:text/html;charset=utf-8" );  //统一输出编码为utf-8  
$mysql_server_name = "qdm113416174.my3w.com"; //mysql数据库服务器
$mysql_username = "qdm113416174"; //mysql数据库用户名
$mysql_password = "yang147258369"; //mysql数据库密码
$mysql_database = "qdm113416174_db"; //mysql数据【库】名
$mysql_table = "carts"; //mysql数据【表】名

/** 新建 连接 */
$mysqli = new mysqli($mysql_server_name, $mysql_username, $mysql_password);
/** 调试连接是否正常 */
//var_dump($mysqli);

/** 
 * check connection 
 */
if ($mysqli->connect_errno) {
    index("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}else{
	println("Connect success");
	$mysqli->set_charset("utf8");//设置读、写数据的编码
//	add();
	find();
}

/** 删除 数据库 */
function del_db(){
	$sql_delete_db = "drop database $mysql_database";
	if ($mysqli->query($sql_delete_db) === true) {
	    println("delete db ok");
	} else {
	    println("delete db failed: %s\n", $mysqli->connect_error);
	}
}

/**
 * 创建数据库：db
 */
function create_db(){
	$sql_create_db = "create database $mysql_database";
	if ($mysqli->query($sql_create_db) === true) {
	    println("create db ok");
	} else {
	    println("create db failed:" . $mysqli->error);
	}
}

/**
 * 选择数据库；db
 */
function choose_db(){
	$mysqli->select_db($mysql_database);
}

/**
 * 创建数据表；
 */
function create_table(){
	$sql_create_table = "create table $mysql_table(id int NOT NULL AUTO_INCREMENT,PRIMARY KEY(id),name varchar(15),age int)";
	if ($mysqli->query($sql_create_table) === true) {
	    println("create table ok");
	} else {
	    println("create table failed:" . $mysqli->error);
	}
}

/**
 * 从表中删除数据；
 */
function del(){
	$sql_delete = "delete from $mysql_table where age = 200";
	if ($mysqli->query($sql_delete) === true) {
	    println("delete table data ok");
	} else {
	    println("delete table data failed:" . $mysqli->error);
	}
}

/**
 * 在表中插入新数据；
 */
function add(){
	global $mysqli,$mysql_database,$mysql_table;
	$age = rand(12, 80);//随机生成年龄
	$mysqli->select_db($mysql_database);
	$sql_inset = "insert into $mysql_table (idcard,content) value ('车牌','电池')";
	if ($mysqli->query($sql_inset) === true) {
	    println("insert table ok");
	} else {
	    println("insert table failed:" . $mysqli->error);
	}
}

/**
 * 从表中查询数据；
 */
function find(){
	global $mysqli,$mysql_database,$mysql_table;
	$mysqli->select_db($mysql_database);
	println("查询数据库");
	$sql_select = "select * from  $mysql_table order by id";
	if ($result = $mysqli->query($sql_select)) {
	    println("总条数：".$result->num_rows);
	    
	    println("id &nbsp;&nbsp; idcard  &nbsp;&nbsp; content  &nbsp;&nbsp;  创建时间  &nbsp;&nbsp;  修改时间");
	    while ($row = $result->fetch_row()) {
	    	$arr['data_content'][] = $row;
	        // println("$row[0]  $row[1]  $row[2] $row[3]  $row[4]");
	    }
		println(json_encode($arr));
	    $result->close();
	}
}

/**
 * 更新表中数据；
 */
function update(){
	$sql_update = "update $mysql_table set age = 200 where age < 67";
	if ($mysqli->query($sql_update) === true) {
	    println("sql_update table ok");
	} else {
	    println("sql_update table failed:" . $mysqli->error);
	}
}
/**
 * 关闭连接
 */
$mysqli->close();

/**
 * 打印函数
 */
function println($msg)
{
    echo "<br/>";
    echo $msg;
}
